package com.orcamento.entity.report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.orcamento.entity.Category;
import com.orcamento.entity.enums.Month;
import com.orcamento.entity.report.subreport.CategoryGain;
import com.orcamento.entity.report.subreport.CategorySpent;
import com.orcamento.util.Money;

public class CategoryReport extends Report {

	private List<CategoryGain> moreGains = new ArrayList<CategoryGain>();
	private List<CategorySpent> moreSpents = new ArrayList<CategorySpent>();
	private List<CategorySpent> moreCreditCardsSpents = new ArrayList<CategorySpent>();
	private List<CategorySpent> moreCashSpents = new ArrayList<CategorySpent>();
	
	public CategoryReport(Month month, Integer year) {
		super(month, year);
	}

	@Override
	public void generate() {
		generateCategoriesWithMoreGainsReport();
		generateCategoriesWithMoreSpentsReport();
		generateCategoriesWithMoreCashSpentsReport();
		generateCategoriesWithMoreCreditCardSpentsReport();
	}

	private void generateCategoriesWithMoreCreditCardSpentsReport() {
		Money totalSpentAllCategories = new Money();
		
		for( Category category : categories ) {
			Money totalCreditCardSpentCategory = category.getTotalCreditCardSpents(month, year);
			totalSpentAllCategories = totalSpentAllCategories.plus( totalCreditCardSpentCategory );
			
			moreCreditCardsSpents.add( new CategorySpent(category.getName(), totalCreditCardSpentCategory) );
		}
		
		for( CategorySpent categorySpent : moreCreditCardsSpents ) {
			categorySpent.setPercentage(totalSpentAllCategories);
		}
		
		Collections.sort( moreCreditCardsSpents, Collections.reverseOrder() );
	}

	private void generateCategoriesWithMoreCashSpentsReport() {
		Money totalSpentAllCategories = new Money();
		
		for( Category category : categories ) {
			Money totalCashSpentCategory = category.getTotalCashSpents(month, year);
			totalSpentAllCategories = totalSpentAllCategories.plus( totalCashSpentCategory );
			
			moreCashSpents.add( new CategorySpent(category.getName(), totalCashSpentCategory) );
		}
		
		for( CategorySpent categorySpent : moreCashSpents ) {
			categorySpent.setPercentage(totalSpentAllCategories);
		}
		
		Collections.sort( moreCashSpents, Collections.reverseOrder() );
	}

	private void generateCategoriesWithMoreSpentsReport() {
		Money totalSpentAllCategories = new Money();
		
		
		for( Category category : categories ) {
			Money totalSpentCategory = category.getTotalSpents(month, year);
			totalSpentAllCategories = totalSpentAllCategories.plus( totalSpentCategory );
			
			moreSpents.add( new CategorySpent(category.getName(), totalSpentCategory) );
		}
		
		for( CategorySpent categorySpent : moreSpents ) {
			categorySpent.setPercentage(totalSpentAllCategories);
		}
		
		Collections.sort( moreSpents, Collections.reverseOrder() );
	}

	private void generateCategoriesWithMoreGainsReport() {
		Money totalGainAllCategories = new Money();
		
		for( Category category : categories ) {
			Money totalGainCategory = category.getTotalGain(month, year);
			totalGainAllCategories = totalGainAllCategories.plus( totalGainCategory );
			
			moreGains.add( new CategoryGain( category.getName(), totalGainCategory ) );
		}
		
		for( CategoryGain categoryGain : moreGains ) {
			categoryGain.setPercentage(totalGainAllCategories);
		}
		
		Collections.sort( moreGains, Collections.reverseOrder() );
	}

	public List<CategoryGain> getMoreGains() {
		return moreGains;
	}

	public List<CategorySpent> getMoreSpents() {
		return moreSpents;
	}

	public List<CategorySpent> getMoreCreditCardsSpents() {
		return moreCreditCardsSpents;
	}

	public List<CategorySpent> getMoreCashSpents() {
		return moreCashSpents;
	}
	
}
